<?php
/*商城相关数据操作*/
class Shop extends MySql{
	
	/*新增购物车
	$id   number p_id
	$num  number 数量
	*/
	function addCart($p_id,$num){
		$id = Product::getProductId($p_id);
		$data = array("product" => $id,
							  "create_time" =>  date("Y-m-d H:i:s",time()),
							  "create_user" => $_SESSION['login_mail'],
							  "product_num" => $num);
		if(User::checkLogin()){
			$status = $this->query($this->insert('speed_cart',$data),'none');
			return $status;
		}else return false;
	}
	
	function delCart($id){
		if(User::checkLogin()){
			$user = $this->query($this->select('create_user','speed_cart','id',$id),'one');
			if($user['create_user'] == $_SESSION['login_mail']){
				$status = $this->query($this->del('speed_cart','id',$id),'none');
				return $status;
			}else return false;
		}else return false;
	}
	
	function getCart(){
		if(User::checkLogin()){
			$data = $this->query($this->select('*','speed_cart','create_user',$_SESSION['login_mail']),'arr');
			for($n=0;$n < count($data);$n++){
				$info = $this->query($this->select('id,p_id,name,color,price,path1','speed_product','id',$data[$n]['product']),'one');
				$data[$n]['info'] = $info;
			}
			return $data;
		}else return false;
	}
	
	function updateCartNum($id,$num){
		if(User::checkLogin()){
			$user = $this->query($this->select('create_user','speed_cart','id',$id),'one');
			if($user['create_user'] == $_SESSION['login_mail']){
				$rewrite = array("product_num" => $num);
				$status = $this->query($this->update('speed_cart',$rewrite,'id',$id),'none');
				return $status;
			}else return false;
		}else return false;
	}
	
	function addOrder($data){
		if(User::checkLogin()){
			$id = $this->query($this->insert('speed_orders',$data),'id');
			$cart =  $this->query($this->select('*','speed_cart','create_user',$_SESSION['login_mail']),'arr');
			$sta = false;
			for($n=0;$n< count($cart);$n++){
				$d = array("order_id" => $id,
								 "product_id" => $cart[$n]['product'],
								 "create_time" => date("Y-m-d H:i:s",time()),
								 "p_num" => $cart[$n]['product_num']);
				$sta = $this->query($this->insert('speed_order_pro',$d),'none');
			}
			if($sta)$sta = $this->query($this->del('speed_cart','create_user',$_SESSION['login_mail']),'none');
			$data = $this->query($this->select('orders_id','speed_orders','id',$id),'one');
			return $data['orders_id'];
		}else return false;
	}
	
	function updateOrder($re,$field,$val){
		if(User::checkLogin()){
			$data = $this->query($this->select('create_user','speed_orders',$field,$val),'one');
			if($data['create_user'] == $_SESSION['login_mail']){
				$sta = $this->query( $this->update('speed_orders',$re,$field,$val),'none');
				return $sta;
			}else return false;
		}else return false;	
	}
	
	function adminUpdateOrder($re,$field,$val){
		$sta = $this->query( $this->update('speed_orders',$re,$field,$val),'none');
		return $sta;		
	}
	
	function getOrderById($o_id){
		if(User::checkLogin()){
			$data = $this->query($this->select('*','speed_orders','orders_id',$o_id),'one');
			if($data['create_user'] == $_SESSION['login_mail']){
				$ad = $this->getAddressById($data['address']);
				$data['address'] = $ad;
				$pro = $this->getOrderProduct($data['id']);
				$data['product'] = $pro;
				return $data;
			}else return false;
		}else return false;		
	}
	
	function getOrderByUser($ac='do'){
		if(User::checkLogin()){
			if($ac == 'do')$sql = "select * from speed_orders where `create_user`='".$_SESSION['login_mail']."' and `status`!=7 order by id desc";
			if($ac == 'close')$sql = "select * from speed_orders where `create_user`='".$_SESSION['login_mail']."' and `status`=7 order by id desc";
			if($ac == 'complete')$sql = "select * from speed_orders where `create_user`='".$_SESSION['login_mail']."' and `status`=4 order by id desc";
			$data = $this->query($sql,'arr');
			$d = array();
			for($n=0;$n< count($data);$n++){
				$d[$n] = $data[$n];
				$pp = $this->query($this->select('*','speed_order_pro','order_id',$data[$n]['id']),'arr');
				for($m=0;$m< count($pp);$m++){
					$p = $this->query($this->select('id,name,price,p_id,color,type,path1','speed_product','id',$pp[$m]['product_id']),'one');
					$p['num'] = $pp[$m]['p_num'];
					$d[$n]['product'][$m] = $p;
				}
			}
			return $d;
		}else return false;		
	}
	
	function getOrdersByAction($ac='do'){
		if($ac == 'do')$sql = "select * from speed_orders where `status`!=7 and `status`!=0 and `status`!=4 order by id desc";
		if($ac == 'wait')$sql = "select * from speed_orders where `status`=1 order by id desc";
		if($ac == 'confirm')$sql = "select * from speed_orders where `status`=2 order by id desc";
		if($ac == 'today'){
			$time = date('Y-m-d',time());
			$sql = "select * from speed_orders where `complete_time` like '%$time%' and `status`=4 order by id desc";
		}
		$data = $this->query($sql,'arr');
		$d = array();
		for($n=0;$n< count($data);$n++){
			$d[$n] = $data[$n];
			$pp = $this->query($this->select('*','speed_order_pro','order_id',$data[$n]['id']),'arr');
			for($m=0;$m< count($pp);$m++){
				$p = $this->query($this->select('id,name,price,p_id,color,type,path1','speed_product','id',$pp[$m]['product_id']),'one');
				$p['num'] = $pp[$m]['p_num'];
				$d[$n]['product'][$m] = $p;
			}
		}
		return $d;
	}
	
	function getCartProduct(){
		if(User::checkLogin()){
			$cart =  $this->query($this->select('*','speed_cart','create_user',$_SESSION['login_mail']),'arr');
			$d = array();
			for($n=0;$n< count($cart);$n++){
				$product = $this->query($this->select('id,name,price,color,type','speed_product','id',$cart[$n]['product']),'one');
				$d[$n] = $product;
				$d[$n]['num'] = $cart[$n]['product_num'];
			}
			return $d;
		}else return false;
	}
	
	function getCartTotal(){
		$product = $this->getCartProduct();
		$total = 0;
		for($n=0;$n< count($product);$n++){
			$total+=$product[$n]['price']*$product[$n]['num'];
		}
		return $total;
	}
	
	function checkUser(){
		if(User::checkLogin())return true;
		else{
			$u = '/login/login';
			echo "<script type='text/javascript'>alert('登陆超时，请重新登陆');location.href='$u'</script>";
		}
	}
	
	function getOrderProduct($o_id){
		$pro = $this->query($this->select('*','speed_order_pro','order_id',$o_id),'arr');
		foreach($pro as $val){
			$pp = $this->query($this->select('*','speed_product','id',$val['product_id']),'one');
			$pp['num'] = $val['p_num'];
			$data[] = $pp;
		}
		return $data;
	}
	
	function getUserAddress(){
		if(User::checkLogin()){
			$data = $this->query($this->select('*','speed_address','create_user',$_SESSION['login_mail']),'arr');
			return $data;
		}else return false;
	}
	
	function getAddressById($id){
		$address = $this->getUserAddress();
		foreach($address as $val){
			if($val['id'] == $id)return $val;
		}return false;
	}
	
	function addAddress($data,$ac=''){
		if(User::checkLogin()){
			if($ac == 'id'){
				$sta = $this->query($this->insert('speed_address',$data),'id');
				return $sta;
			}
			$sta = $this->query($this->insert('speed_address',$data),'none');
			return $sta;
		}else return false;
	}
	
	function delAddress($id){
		if(User::checkLogin()){
			$data = $this->query($this->select('create_user','speed_address','id',$id),'one');
			if($data['create_user'] == $_SESSION['login_mail']){
				return $this->query($this->del('speed_address','id',$id),'none');
			}else return false;
		}else return false;	
	}
	
	function getPlace($id=1,$ac='son'){
		if($ac == 'self')return $this->query($this->select('region_id,region_name,parent_id,region_type','speed_region','region_id',$id),'one');
		$data = $this->query($this->select('region_id,region_name,parent_id,region_type','speed_region','parent_id',$id),'arr');
		$d = array();
		for($n=0;$n< count($data);$n++){
			$d[] = array("id" => $data[$n]['region_id'],"name" => $data[$n]['region_name'],"parent" => $data[$n]['parent_id'],"type" => $data[$n]['region_type']);
		}	
		return $d;
	}
	
}
?>